
import threading
import game_thread
import pickle
import proxy_sender

MAX_BUFSIZE = 2 ** 20 #1 MB ought to be enough

class ProxyReceiver(threading.Thread):
    def __init__(self, name, dest_q, incoming_conn):
        super(ProxyReceiver, self).__init__(name = '%s Proxy Receiver' % repr(name) )
        self.conn = incoming_conn
        self.dest_q = dest_q

    def run(self):
        while 1:
            buf = self.conn.recv(MAX_BUFSIZE)
            msg = pickle.loads(buf)
#            print '%s: received %d bytes: %s' % (self.name, len(buf), repr(msg))
            (trans_id, cmd, cmd_args) = msg

            if cmd == 'register_client':
                game_thread.GameThread.respond(self.dest_q, trans_id, cmd, cmd_args[ 0 ], proxy_sender.QueueWrapper(self.conn))
            else:
                game_thread.GameThread.respond(self.dest_q, trans_id, cmd, *cmd_args)
